<?php

namespace app\api\controller;

use app\admin\model\Banner;
use app\admin\model\Classify;
use app\admin\model\Goods;
use app\admin\model\GoodsSkuValue;
use app\admin\model\GoodsSpe;
use app\admin\model\Guarantee;
use app\common\controller\Api;
use think\Env;

class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    //轮播
    public function banner()
    {
        $model = new Banner();
        $list = $model
            ->where('type', 1)
            ->field('id,image')
            ->select();
        foreach ($list as $v) {
            $v['image'] = Env::get('app.yuming') . $v['image'];
        }
        return sucessMsg('200', '获取成功', $list);
    }

    //商品保障（轮播图下面）
    public function guarantee()
    {
        $model = new Guarantee();
        $list = $model
            ->field('id,name')
            ->select();
        return sucessMsg('200', '获取成功', $list);
    }

    //限时优惠
    public function flashSale()
    {
        $model = new Goods();
        $list = $model
            ->where('is_discounts', 1)
            ->field('id,title,desc,price,discounts_price,image')
            ->select();
        foreach ($list as $v) {
            $v['iamge'] = Env::get('app.yuming') . $v['image'];
        }
        return sucessMsg('200', '获取成功', $list);
    }

    //商品分类（首页）
    public function classify()
    {
        $model = new Classify();
        $list = $model
            ->field('id,name')
            ->select();
        return sucessMsg('200', '获取成功', $list);
    }

    //商品列表
    public function goods()
      {
        $cate_id = input('classify_id');
        $list = Goods::where('classify_id', $cate_id)
            ->field('id,title,desc,price,discounts_price,image,classify_id')
            ->select();
        foreach ($list as $v) {
            $v['image'] = Env::get('app.yuming') . $v['image'];
        }
        return sucessMsg('200', '获取成功', $list);
    }

     //首页商品详情页
    public function goodsDetails()
    {
        $id = input('id/d');
        if (empty($id)) {
            return errMsg(422, '请输入商品id!');
        }
        $model = new Goods();
        $list = $model
            ->where('id', $id)
            ->field('id,image,title,desc,price,discounts_price,shipments,content')
            ->find();
        if (empty($list)) {
            return errMsg(422, '找不到数据');
        }
        $list->image = Env::get('app.yuming') . $list->image;
        return sucessMsg(200, '详情 获取成功！', $list);
    }
    //商品规格
    public function specification(){
        $id = input('goods_id');
        if(empty($id)){
            return errMsg(401,'请输入商品ID');
        }
        $spe = GoodsSpe::where('goods_id',$id)
            ->field('id,price,image,value_ids')
            ->select();
        foreach ($spe as $v){
            $value = GoodsSkuValue::where('id','in',$v['value_ids'])
                ->field('id,name,sku_id')
                ->select();
            $v['image'] = Env::get('app.yuming') . $v['image'];
        }
        return sucessMsg(200,'获取成功',[
            'spe' => $spe,
            'value' => $value
        ]);
    }
}